//////////////////////////////////////////////////////////////////////////////////////
//
//  Copyright (c) 2014-present, Egret Technology.
//  All rights reserved.
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions are met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the Egret nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
//  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
//  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
//  IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
//  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
//  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
//  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
//  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////////////////////

class Main extends egret.DisplayObjectContainer {



    public constructor() {
        super();
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
    }

    private onAddToStage(event: egret.Event) {

        egret.lifecycle.addLifecycleListener((context) => {
            // custom lifecycle plugin

            context.onUpdate = () => {

            }
        })

        egret.lifecycle.onPause = () => {
            egret.ticker.pause();
        }

        egret.lifecycle.onResume = () => {
            egret.ticker.resume();
        }

        this.runGame().catch(e => {
            console.log(e);
        })



    }

    private async runGame() {
        await this.loadResource()
        this.createGameScene();
        const result = await RES.getResAsync("description_json")
        this.startAnimation(result);
        await platform.login();
        const userInfo = await platform.getUserInfo();
        console.log(userInfo);

    }

    private async loadResource() {
        try {
            const loadingView = new LoadingUI();
            this.stage.addChild(loadingView);
            await RES.loadConfig("resource/default.res.json", "resource/");
            await RES.loadGroup("preload", 0, loadingView);
            this.stage.removeChild(loadingView);
        }
        catch (e) {
            console.error(e);
        }
    }

    private textfield: egret.TextField;

    /**
     * 创建游戏场景
     * Create a game scene
     */
    private createGameScene() {
       let bt1:egret.Bitmap=new  egret.Bitmap()
        bt1.texture=RES.getRes('bg_png');
        bt1.width=560;
        bt1.height=600;
        bt1.x=40;
        bt1.y=200;
        this.addChild(bt1);

        let txt:egret.TextField=new egret.TextField();
        txt.text="登录"
        txt.size=80;
        txt.bold=true;
        txt.x=230;
        txt.y=300;
        txt.textColor=0x000000;
        this.addChild(txt);

        let txt1:egret.TextField=new egret.TextField();
        txt1.text="账号"
        txt1.size=50;
       // txt1.bold=true;
        txt1.x=100;
        txt1.y=400;
        txt1.textColor=0x000000;
        this.addChild(txt1);
        
        let txt5: egret.TextField = new egret.TextField();
        this.addChild(txt5);
        txt5.type = egret.TextFieldType.INPUT;
        txt5.width = 300;
        txt5.height = 50;
        txt5.border = true;
        txt5.borderColor = 0x000000;
        txt5.size = 50;
        txt5.textColor = 0x0000ff;
        txt5.x = 240;
        txt5.y = 400;
      

        let txt2:egret.TextField=new egret.TextField();
        txt2.text="密码"
        txt2.size=50;
       // txt1.bold=true;
        txt2.x=100;
        txt2.y=500;
        txt2.textColor=0x000000;
        this.addChild(txt2);

         let txt6: egret.TextField = new egret.TextField();
        this.addChild(txt6);
        txt6.type = egret.TextFieldType.INPUT;
        txt6.width = 300;
        txt6.height = 50;
        txt6.border = true;
        txt6.borderColor = 0x000000;
        txt6.size = 50;
        txt6.textColor = 0x0000ff;
        txt6.x = 240;
        txt6.y = 500;

        let txt3:egret.TextField=new egret.TextField();
        txt3.text="确认登录"
        txt3.size=80;
       // txt1.bold=true;
        txt3.x=160;
        txt3.y=600;
        txt3.textColor=0x000000;
        this.addChild(txt3);
        txt3.touchEnabled=true;
        txt3.addEventListener(egret.TouchEvent.TOUCH_TAP,myfuntion,this)

       
        let main1:egret.DisplayObjectContainer=new egret.DisplayObjectContainer();
        this.addChild(main1);

        let bt2:egret.Bitmap=new  egret.Bitmap()
        bt2.texture=RES.getRes('bg_png');
        bt2.width=400;
        bt2.height=400;
        bt2.x=120;
        bt2.y=300;
        bt2.visible=false;
        main1.addChild(bt2);

        let bt3:egret.Bitmap=new  egret.Bitmap()
        bt3.texture=RES.getRes('close_png');
        bt3.width=70;
        bt3.height=70;
        bt3.x=440;
        bt3.y=320;
        bt3.visible=false;
        main1.addChild(bt3);
        bt3.touchEnabled=true;
        bt3.addEventListener(egret.TouchEvent.TOUCH_TAP,myfuntion1,this)

        let text:egret.TextField=new egret.TextField();
        text.textFlow = <egret.ITextElement[]>[
            {text:"亲爱的小伙伴们:\n",style:{textColor:0x0000ff,lineSpacing:20}},
            {text:"本书即将上线\n",style:{textColor:0xfeae01,size:50,lineSpacing:20}},
            {text:"感谢小伙伴们\n",style:{textColor:0x0000ff,size:50,lineSpacing:20}},
            {text:"向上滑动鼠标\n",style:{textColor:0x000000,size:35,lineSpacing:20}},
            {text:"-------------\n",style:{textColor:0x000000}},
            {text:"抵制不良游戏\n",style:{textColor:0x000000,size:35,lineSpacing:20}},
            {text:"拒绝盗版游戏\n",style:{textColor:0x000000,size:35,lineSpacing:20}},
            {text:"注意自我保护\n",style:{textColor:0x000000,size:35,lineSpacing:20}},
            {text:"谨防受骗上当\n",style:{textColor:0x000000,size:35,lineSpacing:20}},
            {text:"适度游戏益脑\n",style:{textColor:0x000000,size:35,lineSpacing:20}},
            {text:"沉迷游戏伤身\n",style:{textColor:0x000000,size:35,lineSpacing:20}},
            {text:"合理安排时间\n",style:{textColor:0x000000,size:35,lineSpacing:20}},
            {text:"享受健康生活\n",style:{textColor:0x000000,size:35,lineSpacing:20}}   
        ]
        var myscrollView:egret.ScrollView = new egret.ScrollView();
        myscrollView.setContent(text);
        myscrollView.width = 400;
        myscrollView.height = 250;
        myscrollView.x =150;
        myscrollView.y = 360;
        main1.addChild(myscrollView);
        myscrollView.visible=false;

        function myfuntion(){
             bt2.visible=true;
              myscrollView.visible=true;
              bt3.visible=true;
        }
        function myfuntion1(){
            bt2.visible=false;
              myscrollView.visible=false;
              bt3.visible=false;
        }
    }
}